package com.zkingsoft.actions.admin.bus;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zkingsoft.anotations.RemoveRequestToken;
import com.zkingsoft.anotations.SaveRequestToken;
import com.zkingsoft.authority.Authority;
import com.zkingsoft.constance.SystemConstance;
import com.zkingsoft.constraint.BaseController;
import com.zkingsoft.model.bus.BusParameterSettings;
import com.zkingsoft.model.sys.SysUsers;
import com.zkingsoft.pojo.AjaxResult;
import com.zkingsoft.pojo.PaginationVO;
import com.zkingsoft.services.bus.BusParameterSettingsService;
import com.zkingsoft.util.WebUtil;

import pagerVo.ParamVo;

/**
 * This field was generated by Zking.software.Codegen.
 * 
 * @date 2016-11-28 15:14
 */
@Controller
@RequestMapping(value = "admin/busParameterSettings")
public class BusParameterSettingsController extends BaseController {

	@Resource
	private BusParameterSettingsService busParameterSettingsService;

	@Resource(name = "authorityAdapter")
	private Authority authorityAdapter;

	// 记录编辑前的值Before_Edit_Value
	public static final String BEV = "BusParameterSettings_BEV";

	public static final String fnCode = "BusParameterSettings";
	public static final String search = fnCode + ":search";
	public static final String edit = fnCode + ":edit";
	public static final String del = fnCode + ":del";
	public static final String add = fnCode + ":add";

	/**
	 * 列表显示
	 */
	@RequestMapping(value = "/showList")
	public @ResponseBody AjaxResult showList(BusParameterSettings busParameterSettings, PaginationVO pageVo) {
		authorityAdapter.isBtnPermitted(search);
		SysUsers user = authorityAdapter.getLoginUser();
		busParameterSettings.setCompanyId(user.getCompanyId());
		return showList(busParameterSettingsService, busParameterSettings, pageVo);
	}

	/**
	 * 新增或者修改页面
	 */
	@RemoveRequestToken
	@RequestMapping(value = "/addOrModify")
	public @ResponseBody AjaxResult addOrModify(BusParameterSettings busParameterSettings) {
		if (busParameterSettings.getParamId() != null) {
			authorityAdapter.isBtnPermitted(edit);
			AjaxResult result = modify(busParameterSettingsService, WebUtil.getSessionAttribute(BEV),
					busParameterSettings, "busParameterSettings");
			WebUtil.removeSessionAttribute(BEV);
			return result;
		} else {
			authorityAdapter.isBtnPermitted(add);
			SysUsers user = authorityAdapter.getLoginUser();
			busParameterSettings.setCompanyId(user.getCompanyId());
			return add(busParameterSettingsService, busParameterSettings, "busParameterSettings");
		}
	}

	/**
	 * 进入修改界面
	 */
	@SaveRequestToken
	@RequestMapping(value = "/editForm")
	public String editForm(Long id) {
		authorityAdapter.isBtnPermitted(edit);
		BusParameterSettings busParameterSettings;
		if (id != null) {
			busParameterSettings = busParameterSettingsService.findById(id);
			WebUtil.getRequest().setAttribute("obj", busParameterSettings);
			WebUtil.setSessionAttribute(BEV, busParameterSettings);
		}
		return "admin/bus/parameterSetting-form";
	}

	/**
	 * 删除
	 */
	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(String keys) {
		authorityAdapter.isBtnPermitted(del);
		return remove(busParameterSettingsService, keys);
	}

	/**
	 * 批量修改web配置
	 */
	@RequestMapping(value = "/modifyWebParam")
	public @ResponseBody AjaxResult modifyWebParam(ParamVo paramVo) {
		authorityAdapter.isBtnPermitted(edit);
		for (BusParameterSettings param : paramVo.getParam()) {
			busParameterSettingsService.modifyByModel(param);
			// 同时更新session
			WebUtil.setSessionAttribute(param.getParamValue2(), param.getParamValue());
		}
		return new AjaxResult(AjaxResult.STATUS_OK, "更新成功");
	}

	/**
	 * 加入批量修改web配置
	 */
	@RequestMapping(value = "/toWebParam")
	public String toWebParam(Long id) {
		authorityAdapter.isBtnPermitted(edit);
		BusParameterSettings query = new BusParameterSettings();
		query.setParamValue1(SystemConstance.CMSPZ);
		SysUsers user = authorityAdapter.getLoginUser();
		query.setCompanyId(user.getCompanyId());
		List<BusParameterSettings> list = busParameterSettingsService.findByModel(query);
		WebUtil.getRequest().setAttribute("list", list);
		return "admin/web/webSet-form";
	}

}
